package cn.smbms.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.smbms.pojo.Comments;
import cn.smbms.pojo.Order_detail;
import cn.smbms.pojo.User;
import cn.smbms.service.order_detail.OrderDetailService;
import cn.smbms.tools.Constants;

@Controller
@RequestMapping(value = "/comments")
public class CommentsController {
	@Resource
	private OrderDetailService orderDetailService;

	// 点击待评价进入需要评价订单列表
	@RequestMapping(value = "/comprolist.html")
	public String getcomprolist(Model model, HttpSession session) {
		int userid = ((User) session.getAttribute(Constants.USER_SESSION))
				.getId();
		List<Order_detail> list = orderDetailService.getDetailByshou(userid);
		model.addAttribute("list", list);
		return "daipingjia";
	}

	// 评论页面跳转
	@RequestMapping(value = "/assess.html")
	public String comment(@RequestParam(value = "pid") Integer pid,
			@RequestParam(value = "orderid") Integer orderid,
			@RequestParam(value = "fileName") String fileName, Model model,
			HttpSession session, @ModelAttribute("comments") Comments comments) {
		int userid = ((User) session.getAttribute(Constants.USER_SESSION))
				.getId();
		model.addAttribute("uid", userid);
		model.addAttribute("pid", pid);
		model.addAttribute("orderid", orderid);
		model.addAttribute("fileName", fileName);
		return "assess";
	}

	// 点击添加评论
	@RequestMapping(value = "/addcomm.html", method = RequestMethod.POST)
	public String add(@Valid Comments comments) {

		DateFormat d1 = DateFormat.getDateInstance(); // 默认语言（汉语）下的默认风格（MEDIUM风格，比如：2008-6-16
		Date now = new Date();
		String str1 = d1.format(now);
		comments.setCdate(str1);
		boolean flag = orderDetailService.addcomment(comments);
		if (flag == true) {
			return "redirect:/user/sys/self.html";
		}
		return "daipingjia";

	}
}
